using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using OurMediaPlayer.View;

namespace OurMediaPlayer
{
    public partial class PlaylistForm : SliderForm
    {
        public MainForm mainForm;

        public PlaylistForm()
        {
            InitializeComponent();
        }

        public PlaylistForm(Form owner, float fStep):base(owner,fStep)
        {
            this.mainForm = (MainForm)owner;
            this.SlideDirection = ESlideDirection.Right;
            InitializeComponent();
        }

        private void addFileMenu_Click(object sender, EventArgs e)
        {
            // Opens the openFileDialog
            if (openFileDialog.ShowDialog() == DialogResult.Cancel)
                return;

            

            // Open new file
            try
            {
                //Creates the new video
                Media newVideo = new Media(this.openFileDialog.FileName);
                
                //Loads the new video
                newVideo.Load(Double.Parse(this.intervalTextBox.Text));

                if(!this.mainForm.mediaManager.videos.Contains(this.openFileDialog.FileName))
                    this.playlistBox.Items.Add(this.openFileDialog.FileName);

                //Adds new video to the videoManager
                this.mainForm.mediaManager.AddVideo(newVideo, this.openFileDialog.FileName);
            }
            catch (COMException ce)
            {
                MessageBox.Show("Failed to open file: " + ce.Message, "Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void removeFileMenu_Click(object sender, EventArgs e)
        {
            if (this.playlistBox.SelectedItem != null)
            {
                //Gets the video
                Media video = this.mainForm.mediaManager.GetVideo((string)this.playlistBox.SelectedItem);

                //If the video is the current playing video
                if (video == this.mainForm.mediaManager.currentMedia)
                {
                    this.mainForm.StopVideo();
                    this.mainForm.mediaManager.CloseCurrentVideo();
                }

                //Reload Info
                this.mainForm.tracksForm.ReloadInfo();

                //Removes video from videoManager
                this.mainForm.mediaManager.RemoveVideo((string)this.playlistBox.SelectedItem);

                //Removes video index from playlist
                if (this.playlistBox.SelectedItem != null)
                    this.playlistBox.Items.Remove(this.playlistBox.SelectedItem);
            }
        }

        private void playlistBox_DoubleClick(object sender, EventArgs e)
        {
            //If an item was double-clicked
            if ( this.playlistBox.SelectedItem != null)
            {
                //Stops previous video
                this.mainForm.StopVideo();

                //Clears the assossiation between that video and current control
                if(this.mainForm.mediaManager.currentMedia != null)
                    this.mainForm.mediaManager.currentMedia.ClearVideoWindow() ;

                //Clears filters
                if (this.mainForm.filtersManager.currentFilter != null)
                    this.mainForm.filtersManager.currentFilter = null;

                //Sets Selected Item as new Video
                this.mainForm.mediaManager.SetCurrentVideo((string)this.playlistBox.SelectedItem);

                //Opens video
                this.mainForm.OpenVideo();

                //Reloads TracksForm info
                this.mainForm.tracksForm.ReloadInfo();

                //Associates newVideo with moviePanel on MainForm
                this.mainForm.mediaManager.currentMedia.SetupVideoWindow(mainForm.moviePanel);
            }
        }

    }
}